import {http} from "@/core/common/HttpUtil";
import type {CategoryProp} from "@/core/entity/CategoryProp";
import type {User} from "@/core/entity/User";
import {Resume} from "@/core/entity/Resume";
import {ProcessActiveNode} from "@/core/entity/ProcessActiveNode";

/**
 * 退出登录
 */
export function logout() {
    return http.get<User[]>('/sillyAdmin/logout');
}
export function getAllUser() {
    return http.get<User[]>('/sillyAdmin/allUser');
}

export function getAllUserJson() {
    return http.get<string>('/sillyAdmin/allUserJson');
}

export function getAllCategory() {
    return http.get<CategoryProp>('/sillyAdmin/allProperty');
}

export function getAllCategoryJson() {
    return http.get<string>('/sillyAdmin/allPropertyJson');
}

export function todoPage(category: string, params: object) {
    return http.post<any>(`/sillyAdmin/todo?category=${category}`, params);
}

export function historyPage(category: string, params: object) {
    return http.post<any>(`/sillyAdmin/history?category=${category}`, params);
}

export function queryPage(category: string, params: object) {
    return http.post<any>(`/sillyAdmin/query?category=${category}`, params);
}

/**
 * 新增数据
 */
export function addMap(category: string, params: object) {
    return http.post<any>(`/sillyAdmin/addMap?category=${category}`, params);
}

/**
 * 新增或保存数据
 */
export function addOrSaveMap(category: string, params: object) {
    return http.post<any>(`/sillyAdmin/addOrSaveMap?category=${category}`, params);
}

/**
 * 保存数据
 */
export function saveMap(category: string, params: object) {
    return http.post<any>(`/sillyAdmin/saveMap?category=${category}`, params);
}

/**
 * 清除此业务下的缓存数据
 */
export function refreshCache(category: string) {
    return http.post<string>(`/sillyAdmin/refreshCache?category=${category}`);
}

/**
 * 更新SILLY配置信息
 */
export function refreshResource() {
    return http.post<string>(`/sillyAdmin/refreshResource`);
}

/**
 * 获取流程履历数据
 */
export function getResumeList(category: string, masterId: string) {
    return http.get<Resume[]>(`/sillyAdmin/resumeList?category=${category}&masterId=${masterId}`);
}

/**
 * 删除并结束流程
 */
export function deleteMaster(category: string, masterId: string) {
    return http.post<string>(`/sillyAdmin/delete?category=${category}&masterId=${masterId}`);
}

/**
 * 流程图获取
 */
export function modelSourceByProcessKey(category: string, processKey: string) {
    return http.get<string>(`/sillyAdmin/modelSourceByProcessKey?category=${category}&processKey=${processKey}`);
}

/**
 * 流程图获取
 */
export function modelSource(category: string, processId: string) {
    return http.get<string>(`/sillyAdmin/modelSource?category=${category}&processId=${processId}`);
}

/**
 * 流程图获取激活的节点信息
 */
export function activeNodeKey(category: string, processId: string) {
    return http.get<ProcessActiveNode>(`/sillyAdmin/activeNodeKey?category=${category}&processId=${processId}`);
}

/**
 * 任务流转
 */
export function flowTask(category: string, taskId: string, userId: string, reason: string) {
    return http.post<string>(`/sillyAdmin/flow?category=${category}&taskId=${taskId}&userId=${userId}&reason=${reason}`);
}

/**
 * 任务驳回
 */
export function rejectTask(category: string, taskId: string, nodeKey: string, reason: string, userId: string | null, deleteOtherTask: boolean | null) {
    return http.post<string>(`/sillyAdmin/reject?category=${category}&taskId=${taskId}&nodeKey=${nodeKey}&reason=${reason}&userId=${userId}&deleteOtherTask=${deleteOtherTask}`);
}

/**
 * 流程跳转
 */
export function jumpProcess(category: string, masterId: string, nodeKey: string, reason: string, params: object) {
    return http.post<string>(`/sillyAdmin/jumpNode?category=${category}&masterId=${masterId}&nodeKey=${nodeKey}&reason=${reason}`, params);
}

/**
 * 数据修改
 */
export function saveFields(category: string, masterId: string, nodeKey: string, taskId: string, params: object) {
    return http.post<string>(`/sillyAdmin/saveFields?category=${category}&masterId=${masterId}&nodeKey=${nodeKey}&taskId=${taskId}`, params);
}

/**
 * 模型列表
 */
export function modelPage(params: object) {
    return http.post<any>(`/sillyAdmin/modelPage`, params);
}

/**
 * 流程部署
 */
export function processDeploy(options: any) {
    const formData = new FormData();
    formData.append(options.filename, options.file);
    return http.post<any>(`/sillyAdmin/processDeploy`, formData,{
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    }).then(response => {
        console.log(response);
        options.onSuccess(response);
    }).catch(error => {
        options.onError(error);
    });
}

/**
 * 部署信息删除
 */
export function deploymentDelete(deploymentId: string) {
    return http.post<any>(`/sillyAdmin/deploymentDelete?deploymentId=${deploymentId}`);
}

/**
 * 流程定义列表
 */
export function definitionPage(params: object) {
    return http.post<any>(`/sillyAdmin/definitionPage`, params);
}

/**
 * 流程实例列表
 */
export function instancePage(params: object) {
    return http.post<any>(`/sillyAdmin/instancePage`, params);
}